package com.situ.water_read.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.water_read.pojo.RdChangevalue;
import com.situ.water_read.pojo.RdRead;
import com.situ.water_read.service.RdChangevalueService;
import com.situ.water_system.pojo.SyEmp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Angel
 * @since 2020-12-01
 */
@RestController
@RequestMapping("/rd-changevalue")
public class RdChangevalueController {
    @Autowired
    private RdChangevalueService service;
    //修改上月底吗
    @PostMapping("/updatepreVlaue")
    public void updatepreVlaue(RdChangevalue value, HttpSession session){
        SyEmp user = (SyEmp) session.getAttribute("user");
        service.updatePre(value.getRid(),value.getPreValue2());
        if (user==null) {
            int i = service.addChangePreValue(value, 1);
        }else {
            int i = service.addChangePreValue(value, user.getId());
        }
    }
    @GetMapping("/prevalue")
    public IPage<Map> prevalue(RdChangevalue preval, String datepre1, String datepre2, @RequestParam(defaultValue = "",required = false)String date1, @RequestParam(defaultValue ="1",required = false) int current ,
                               @RequestParam(defaultValue = "2",required = false) int size) throws ParseException {
        QueryWrapper wrapper=new QueryWrapper();
        if(preval.getEmpID()!=null){
            wrapper.eq("r.EmpID",preval.getEmpID());
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        if(datepre1!=null && !"".equals(datepre1)){
            Date parse1 = sdf.parse(datepre1);
            wrapper.gt("Datee",parse1);
        }
        if(datepre2!=null && !"".equals(datepre2)){
            Date parse2 = sdf.parse(datepre2);
            wrapper.lt("Datee",parse2);
        }

        Page<Map> page=new Page<>(current,size);
        IPage<Map> iPage = service.querypre(page, wrapper);
        return iPage;
    }
}

